import requests
import string

url_mem = 'https://qun.qq.com/cgi-bin/qun_mgr/search_group_members'
data = {'gc':1078641913,'st':0,'end':40,'sort':0,'bkn':340083496}
headers = {'cookie':'pgv_pvi=1245740032; ptui_loginuin=704096641; RK=4caMv1QSTi; '
                     'ptcz=910ccdbc1db6dc80b9437accef2ab6b2bdcb0c4467e3d10f09ada039efe03efc; uin=o0704096641;'
                     ' pgv_info=ssid=s3739969708; pgv_pvid=3586945441; o_cookie=704096641;'
                     ' pac_uid=1_704096641; _qpsvr_localtk=0.12427418494833398; skey=@W4GdMPWqh; '
                     'p_uin=o0704096641; pt4_token=9623x*UI9fd2NjnUxADENDKhfBJkRj8f1DAaDmhw2tY_; '
                     'p_skey=ivchNq-uYej5KRmx-wBTwAaAytKbIGmXFcmb7Olrp5E_; traceid=d76386f593'}
r = requests.post(url_mem,data=data,headers=headers)

def download():
    url_pic = 'https://q4.qlogo.cn/g?b=qq&nk=857566034&s=140'
    r_pic = requests.get(url_pic,verify=False)

result = r.json()#获取接口返回结果
members = result['mems']#获取返回结果中'mems'
len_members = len(members)#获取返回结果中'mems'list的长度

members_qq = []

for i in range(len_members):

    members_qq.append( members[int(i)]['uin'])#将返回结果中'mems'中的qq号拼接为一个list

    url_down = 'https://q4.qlogo.cn/g?b=qq&nk={}&s=140'.format(members_qq[i])#拼接下载地址
    r_down = requests.get(url_down,verify=False) #请求qq群成员头像接口

    requests.packages.urllib3.disable_warnings()#关闭安全警告

    num_member=members[i]
    if num_member['card']:
        if str(num_member['card']).isidentifier():
            picture_name = str(num_member['card'])+'.jpg'
            f_down = open(picture_name, 'wb')
            f_down.write(r_down.content)

        elif str(num_member['nick']).isidentifier():
            picture_name = str(num_member['nick']) + '.jpg'
            f_down = open(picture_name, 'wb')
            f_down.write(r_down.content)
        else:
            print("{}不符合命名规则,跳过此人".format(members_qq[i]))
            continue
f_down.close()

